package com.zit.java.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.zit.java.pojo.TuitionFee;
import com.zit.java.service.pay.PayService;
import com.zit.java.util.object.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("pay")
public class PaymentController {

    @Autowired
    private PayService payService;


    @RequestMapping("toPayData")
    public String toPayData() {
        return "payment/paymentData";
    }


    @RequestMapping("payData")
    @ResponseBody
    public TableData<TuitionFee> payData() {
        TableData<TuitionFee> tableData = new TableData();
        List<TuitionFee> tuitionFees = payService.selectPayData();
        tableData.setCode(0);
        tableData.setData(tuitionFees);
        tableData.setMsg("ok");
        tableData.setCount(tuitionFees.size());
        return tableData;
    }

    /**
     * 支付弹出支付页面进行支付订单的回显
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("toPaymentPag")
    public String toPaymentPag(Model model, Integer id, HttpSession session) {
        TuitionFee tuitionFee = payService.selectByIdProject(id);
        session.setAttribute("fee",tuitionFee);
        model.addAttribute("tuitionfee", tuitionFee);
        model.addAttribute("random", DateUtil.year(new Date())+RandomUtil.randomNumbers(10) );
        int i = RandomUtil.randomInt();
        if (i > 0) {
            model.addAttribute("orderId", i);
        } else {
            i *= -1;
            model.addAttribute("orderId", i);
        }
        return "alipay/index";
    }


    /**
     * 支付页面
     *
     * @return
     */
    @RequestMapping("/payPag")
    public String toPayPag() {
        return "alipay/alipay.trade.page.pay";
    }

//    /**
//     * 支付成功跳转的页面
//     * 放到数据库
//     * @return
//     */
//    @RequestMapping("/return")
//    public String toReturnUrl(String out_trade_no,String total_amount,String trade_no) {
//        ZhifiPayList zhifiPayList = new ZhifiPayList();
//        zhifiPayList.setOutTradeNo(out_trade_no);
//        zhifiPayList.setTotalAmount(Double.parseDouble(total_amount));
//        zhifiPayList.setTradeNo(trade_no);
//        payService.savePayRecord(zhifiPayList);
//        return "redirect:pay/queryPayData";
//    }
//
//    /**
//     * 到查询支付的数据页面
//     * @return
//     */
//    @RequestMapping("/queryPayData")
//    public String queryAllPayData(){
//        return "alipay/orderlist";
//    }

    /**
     * 订单查询
     * @return
     */
    @RequestMapping("/orderQuery")
    public String orderQuery() {
        return "alipay/alipay.trade.query";
    }

}
